<template>
  <div class="visitor-container">
    <div class="flex-row justify-between">
      <!-- search -->
      <div class="searchBox">
        <el-form inline :model="queryInfo" class="demo-form-inline" label-position="left" label-width="80px">
          <el-form-item label="地图名称">
            <el-select v-model="queryInfo.productTypeId" >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="mg-top-4" style="flex-shrink: 0;">
        <el-button type="info" size="mini" @click="handleQuery">查询</el-button>
        <el-button size="mini" @click="reset">重置</el-button>
      </div>
    </div>
    <add-button title="新增" @addClick="handleAdd" size="mini" />
    <el-table
      v-loading="listLoading"
      :data="tableData" border
      header-cell-class-name="table-header-cell"
      header-row-class-name="table-header-row"
      class="table-container"
    >
      <el-table-column prop="productNumber" label="产品编号" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="productTypeName" label="产品类别" />      
      <el-table-column label="创建时间" width="220" >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | dateTimeFormatter  }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  width="200" >
        <template slot-scope="scope">
           <el-link
            type="primary"
            :underline="false"
            class="upStr fz-1"
            @click="updateDetails(scope.row.id)"
          >
            修改
          </el-link>
          <span class="line">|</span>
          <el-link
            type="danger"
            :underline="false"
            class="upStr redBtn"
            @click="submitDelete(scope.row.id)"
          >
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      :total="total"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="paging.pageSize"
      :current-page="currentPage"
      layout="total, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <!-- 添加或修改 -->
    <el-dialog :title="dialogtit" :visible.sync="dialogFormVisibleAdd" width="30%">
      <el-form ref="pushData" :model="pushData" label-position="right" :rules="rules" class="addForm" label-width="80px" v-loading="formDataLoad">
        <el-form-item label="产品名称"  prop="productName">
          <el-input v-model="pushData.productName"  style="width: 250px;" placeholder="请输入产品名称"  />
        </el-form-item>
        <el-form-item label="产品类别"  prop="productTypeId">
          <el-select v-model="pushData.productTypeId" style="width: 250px;">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit('pushData')">提交</el-button>
        <el-button @click="cancal">取消</el-button>
      </div>
    </el-dialog>
    <!-- 删除车间 -->
  </div>
</template>

<script>
import AddButton from '@/components/add-button/index.vue'
import { findProductTypePageByParam, findProductManagePageByParam, getProductManageById, delProductManageById, addProductManage, updateProductManage } from '@/child-business/api/IndustrialVision' 
export default {
  name: 'ProductManage',
  components: { AddButton },
  data() {
    return {
      dialogtit: '新增',
      listLoading: true,
      showItem: true, // 是否展开
      currentPage: 1, // 默认当前页
      dialogFormVisibleAdd: false, // 新增弹窗
      formDataLoad: false,
      dialogVisible: false, // 提交
      paging: {
        pageNo: 1,
        pageSize: 12
      },
      queryInfo: {
        productName: '',
        productTypeId: ''
      },
      total: 0,
      tableData: [],
      pushData: {
        id: null,
        productName: '',
        productTypeId: ''
      },
      
      rules: {
        productName: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
        productTypeId: [{ required: true, message: '选择对应类别', trigger: 'change' }]
      },
      typeList: []
    }
  },
  computed: {},
  created() {
    this.getList()
    this.gettypelist()
  },
  methods: {
    gettypelist(){
      const find = {
        productTypeVo: {
          typeName: ''
        },
        pageNo: 1,
        pageSize: 12
      }
      findProductTypePageByParam(find).then(response => {
        this.typeList = response.records
        console.log('typelist', response)
      })
    },
    // 获取产品列表
    getList() {
      this.listLoading = true
      findProductManagePageByParam({ productManageVo: this.queryInfo, ...this.paging}).then(response => {
        console.log('records', response)
        this.total = response.total
        this.tableData = response.records
        this.paging.pageNo = response.pageNo
        this.paging.pageSize = response.pageSize
        this.listLoading = false
      })
    },
    // 修改产品信息
    updateDetails(id) {
      this.dialogtit = '修改'
      this.dialogFormVisibleAdd = true
      this.formDataLoad = true
      getProductManageById({ productManageId: id}).then(response => {
        this.pushData.productName = response.productName
        this.pushData.productTypeId = response.productTypeId
        this.pushData.id = response.id
        this.formDataLoad = false
      })
    },
    // 提交添加产品信息
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.pushData.id != null) {
            updateProductManage(this.pushData).then(response => {
              if (response) {
                this.dialogFormVisibleAdd = false
                this.$message({ message: '修改成功！', type: 'success' })                
              }else{
                this.$message({ message: '修改失败！', type: 'error' })       
              }
              this.getList()
            })
          } else {
            delete this.pushData.id
            addProductManage(this.pushData).then(response => {
              if (response) {
                this.dialogFormVisibleAdd = false
                this.$message({
                  message: '消息添加成功！',
                  type: 'success'
                })
                this.getList()
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 取消提交添加产品信息
    cancal() {
      this.dialogFormVisibleAdd = false
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.paging.pageNo = val
      this.getList()
    },
    // 添加产品信息按钮
    handleAdd() {
      this.pushData = {
        typeName: '',
        specification: '',
        unit: ''
      }
      this.dialogFormVisibleAdd = true
    },
    //重置
    reset() {
      this.paging.pageNo = 1
      this.queryInfo.productName = ''
      this.queryInfo.productTypeId = ''
      this.getList()
    },
    // 查询
    handleQuery() {
      this.paging.pageNo = 1
      this.getList()
    },
    // 删除提交
    submitDelete(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delProductManageById({ productManageId: id }).then(response => {
          if (response) {
            this.$message({ message: '删除成功！', type: 'success' })
          } else {
            this.$message({ message: '删除失败！', type: 'error' })
          }
          this.getList()
        })
      }).catch(() => { })
    },
  }
}
</script>
<style lang="scss" scoped>
 @import '@/child-business/styles/IndustrialVision.scss';
</style>
